<?php

namespace api\controller;

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\LoginService;
use common\service\UserCertService;
use common\service\UserService;
use think\Exception;
use think\Request;

class UserController extends VerifyController
{
    private $userService;
    private $loginService;
    private $userCertService;

    public function __construct()
    {
        parent::__construct();
        $this->userService = UserService::getInstance();
        $this->userCertService = UserCertService::getInstance();
        $this->loginService = LoginService::getInstance();
    }

    /**
     * 通过角色code 返回用户列表
     * @param Request $request
     * @return \think\Response
     */
    public function getUserListByRole(Request $request): \think\Response
    {
        try {
            $param = $request->param();
            $res = $this->userService->getUserListByRole($param['param']['filter']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取用户资格证分类列表
     * @param Request $request
     * @return \think\Response
     */
    public function getUserCertCategoryList(Request $request)
    {
        $res = $this->userCertService->getUserCertCategoryList();
        return json(success_response('', $res));
    }

    /**
     * 创建用户资格证分类
     * @param Request $request
     * @return \think\Response|void
     * @throws \Exception
     */
    public function createCertCategory(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->userCertService->createCertCategory($param['data']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改用户资格证分类
     * @param Request $request
     * @return \think\Response
     */
    public function modifyCertCategory(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->userCertService->modifyCertCategory($param['data']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除用户资格证分类
     * @param Request $request
     * @return \think\Response
     */
    public function deleteCertCategory(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->userCertService->deleteCertCategory($param['param']['filter']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取用户资格证
     * @param Request $request
     * @return \think\Response
     */
    public function selectUserCert(Request $request)
    {
        $param = $request->param();
        $res = $this->userCertService->selectUserCert($param['param']['filter']);
        return json(success_response('', $res));
    }

    /**
     * 创建用户资格证
     * @param Request $request
     * @return \think\Response|void
     * @throws \Exception
     */
    public function createUserCert(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->userCertService->createUserCert($param['data']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 更新用户资格证
     * @param Request $request
     * @return \think\Response|void
     * @throws \Exception
     */
    public function modifyUserCert(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->userCertService->modifyUserCert($param['data']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除更新用户资格证
     * @param Request $request
     * @return \think\Response
     */
    public function deleteUserCert(Request $request)
    {
        try {
            $param = $request->param();
            $res = $this->userCertService->deleteUserCert($param['param']['filter']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 设置用户资格证书
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function setUserCert(Request $request)
    {
        $param = $request->param();
        $res = $this->userService->setUserCert($param['data']);
        return json(success_response('', $res));
    }

    /**
     * 查询用户资格证树
     * @param Request $request
     * @return \think\Response
     */
    public function selectUserCertTree(Request $request)
    {
        $param = $request->param();
        $res = $this->userCertService->selectUserCertTree($param);
        return json(success_response('', $res));
    }

    /**
     * 拉取用户信息
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function getUserInfo(Request $request)
    {
        try {
            $res = $this->loginService->getUserInfo($request->getUserId(), false);
            return json(success_response('OK', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 用户列表
     * @param Request $request
     * @return \think\Response
     */
    public function getUserList(Request $request): \think\Response
    {
        try {
            $res = $this->userService->getUserList($request->param()['param']);
            return json(success_response('', $res));
        } catch (\Exception $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}
